package com.hejjon.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;

/**
 * @author: cs
 * @date: 2025-01-28 15:47:21
 * @since: 1.0
 */
public class DateTimeUtil {

    private static final Logger log = LoggerFactory.getLogger(DateTimeUtil.class);


    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String TIME_FORMAT = "YYYYMMddHHmmss";


    // 日期格式
    private static final String[] DATE_FORMATS = {
            "yyyy-MM-dd HH:mm:ss",
            "yyyy-MM-dd HH:mm:ss.SSS",  // 格式1: 2024-08-16 08:01:59.772
            "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", // 格式2: 2024-08-16T08:01:59.772Z
            "yyyy-MM-dd", // 格式3: 2024-08-16
            "HH:mm:ss.SSS", // 格式4: 08:01:59.772
    };


    public static String localDateTime2Str(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ofPattern(DATETIME_FORMAT));
    }

    public static String date2Str(LocalDate localDate) {
        return localDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
    }

    public static String datetime2Str(LocalDateTime localDateTime) {
        return localDateTime.format(DateTimeFormatter.ofPattern(DATETIME_FORMAT));
    }

    public static String date2Str(Date date) {
        return new SimpleDateFormat(DATE_FORMAT).format(date);
    }

    public static String datetime2Str(Date date) {
        return new SimpleDateFormat(DATETIME_FORMAT).format(date);
    }

    public static String datetime2Str(Date date, String dateFormat) {
        return new SimpleDateFormat(dateFormat).format(date);
    }

    /**
     * 日期对象格式化
     * @param dateObj 可能是 java.lang.Date; java.time.LocalDate; java.time.LocalDateTime 类型
     * @param dateFormat 指定的日期格式
     * @return 格式化后的字符串
     */
    public static String dateObjectFormat(Object dateObj, String dateFormat) {
        if (dateObj instanceof Date) {
            return new SimpleDateFormat(dateFormat).format(dateObj);
        } else if (dateObj instanceof LocalDate) {
            LocalDate ld = (LocalDate) dateObj;
            return ld.format(DateTimeFormatter.ofPattern(dateFormat));
        } else if (dateObj instanceof LocalDateTime) {
            LocalDateTime ld = (LocalDateTime) dateObj;
            return ld.format(DateTimeFormatter.ofPattern(dateFormat));
        } else if (dateObj instanceof String) {
            return dateStrFormat(dateObj.toString(), dateFormat);
        }
        return new SimpleDateFormat(dateFormat).format(new Date());
    }



    public static String dateStrFormat(String dateStr, String targetFormat) {
        for (String format : DATE_FORMATS) {
            try {
                LocalDateTime parse = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(format));
                return parse.format(DateTimeFormatter.ofPattern(targetFormat));
            } catch (DateTimeParseException e) {
                log.error("日期转换失败!异常信息: {}", e.getMessage());
            }
        }
        return "";
    }




    /**
     * 转换日期格式  ISO8601格式转成指定格式
     * @param dateStr  2025-10-13T16:00:00.000Z
     * @param newFormat yyyy-MM-dd HH:mm:ss
     * @return 2025-10-13 00:00:00
     */
    public static String convertDateFormatFromISO8601(String dateStr, String newFormat) {
        Date from = Date.from(Instant.parse(dateStr));
        SimpleDateFormat sdf = new SimpleDateFormat(newFormat);
        return sdf.format(from);
    }






}
